package com.saicmotor.pay;

import com.saicmotor.pay.entity.OrderEntity;
import com.saicmotor.pay.entity.PayEntity;
import com.saicmotor.pay.entity.PayResultEntity;
import com.saicmotor.pay.http.OrderPayOkHttpManager;
import com.saicmotor.pay.request.OrderSubmitRequest;
import com.saicmotor.pay.request.PayOrderRequest;
import com.saicmotor.pay.request.QueryPayResultRequest;
import com.zone.sdk.common.http.base.BaseCloudServiceManager;
import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.common.http.callback.ResultCallBack;

import org.jetbrains.annotations.NotNull;

import io.reactivex.observers.DisposableObserver;

public class CloudOrderPayServiceManager extends BaseCloudServiceManager {
    public static CloudOrderPayServiceManager instance;

    private final ICloudOrderPayService iCloudAppStoreService;

    private CloudOrderPayServiceManager(String baseUrl) {
        super();
        iCloudAppStoreService = OrderPayOkHttpManager.getInstance(baseUrl).build(ICloudOrderPayService.class);
    }

    public static CloudOrderPayServiceManager getInstance(String baseUrl) {
        if (instance == null){
            instance = new CloudOrderPayServiceManager(baseUrl);
            return instance;
        }
        return instance;
    }




    /**
     * 订单提交
     * @param request
     * @param callback
     */
    public void submitOrder(OrderSubmitRequest request, ResultCallBack<Result<OrderEntity>> callback){
        observableService.execute(iCloudAppStoreService.submitOrder(request) , new DisposableObserver<Result<OrderEntity>>(){
            @Override
            public void onNext(@NotNull Result<OrderEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 订单支付
     * @param request
     * @param callback
     */
    public void payOrder(PayOrderRequest request, ResultCallBack<Result<PayEntity>> callback){
        observableService.execute(iCloudAppStoreService.payOrder(request) , new DisposableObserver<Result<PayEntity>>(){
            @Override
            public void onNext(@NotNull Result<PayEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 结果查询
     * @param request
     * @param callback
     */
    public void queryPayResult(QueryPayResultRequest request, ResultCallBack<Result<PayResultEntity>> callback){
        observableService.execute(iCloudAppStoreService.queryPayResult(request) , new DisposableObserver<Result<PayResultEntity>>(){
            @Override
            public void onNext(@NotNull Result<PayResultEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }
}
